package ar.com.larctest.adapters;

import android.app.Activity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

import ar.com.larctest.R;
import ar.com.larctest.entities.temp.TMeasurementItem;
import ar.com.larctest.utils.Constants;

public class ListViewMeasurementHorsesAdapter extends ArrayAdapter<TMeasurementItem>
{
    private static final String TAG = "ListViewMeasurementHorsesAdapter";

	public List<TMeasurementItem> items;
	Activity activity;

	public ListViewMeasurementHorsesAdapter(Activity activity, int textViewResourceId, List<TMeasurementItem> items) {
		super(activity, textViewResourceId, items);
		this.items = items;
		this.activity = activity;
	}

	@Override
	public int getCount() {
		return items.size();
	}

	@Override
	public TMeasurementItem getItem(int position) {
		return items.get(position);
	}

	@Override
	public long getItemId(int position) {
		return (items.get(position)).getIdHorse();
	}

	private class ViewHolder {
        ImageView imageState;
	    TextView tvNameHorse;
	    TextView countDown;
        TextView tvLoadedMeasurement;
        ImageView imageSelected;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		LayoutInflater inflater =  activity.getLayoutInflater();
        ViewHolder holder;

		if (convertView == null)
		{
			convertView = inflater.inflate(R.layout.listitem_measurement, null);
			holder = new ViewHolder();
			holder.imageState = (ImageView) (convertView != null ? convertView.findViewById(R.id.imageState) : null);
			holder.tvNameHorse = (TextView) (convertView != null ? convertView.findViewById(R.id.tvNameHorse) : null);
            holder.countDown = (TextView) (convertView != null ? convertView.findViewById(R.id.countDown) : null);
            holder.tvLoadedMeasurement = (TextView) (convertView != null ? convertView.findViewById(R.id.tvLoadedMeasurement) : null);
            holder.imageSelected = (ImageView) (convertView != null ? convertView.findViewById(R.id.imageSelected) : null);

            if (convertView != null) {
                convertView.setTag(holder);
            }
        }
		else
			holder = (ViewHolder) convertView.getTag();

        try
        {
            TMeasurementItem measurementItem = items.get(position);

            if(measurementItem.getState().equals(Constants.STATE_INIT)) {
                holder.tvLoadedMeasurement.setText("");
                holder.imageState.setImageResource(R.drawable.ic_horse_black);
                holder.countDown.setVisibility(View.INVISIBLE);
            }
            else if(measurementItem.getState().equals(Constants.STATE_COACHING))
                holder.imageState.setImageResource(R.drawable.ic_horse_yellow);
            else if(measurementItem.getState().equals(Constants.STATE_FINISH_COACHING)) {
                holder.imageState.setImageResource(R.drawable.ic_horse_green);
                holder.countDown.setVisibility(View.VISIBLE);
                holder.countDown.setText(measurementItem.getTimeOut1());
            }
            else if(measurementItem.getState().equals(Constants.STATE_MEASUREMENT_TIMEOUT)) {
                holder.imageState.setImageResource(R.drawable.ic_horse_red);
                holder.tvLoadedMeasurement.setText(R.string.Measurement_Popup_TimeoutMeasurement);
                holder.countDown.setVisibility(View.VISIBLE);
            }
            else if(measurementItem.getState().equals(Constants.STATE_MEASUREMENT_1)) {
                holder.tvLoadedMeasurement.setText(R.string.Measurement_Popup_1MeasurementLoaded);
                holder.countDown.setText(measurementItem.getTimeOut2());
            }
            else if(measurementItem.getState().equals(Constants.STATE_MEASUREMENT_2)) {
                holder.tvLoadedMeasurement.setText(R.string.Measurement_Popup_2MeasurementLoaded);
                holder.countDown.setText(measurementItem.getTimeOut3());
            }
            else if(measurementItem.getState().equals(Constants.STATE_MEASUREMENT_3)) {
                holder.tvLoadedMeasurement.setText(R.string.Measurement_Popup_3MeasurementLoaded);
                holder.countDown.setVisibility(View.INVISIBLE);
            }
            else if(measurementItem.getState().equals(Constants.STATE_MEASUREMENT_FINAL_DATA)) {
                holder.tvLoadedMeasurement.setText(R.string.Measurement_Popup_FinalDataMeasurementLoaded);
            }

            holder.tvNameHorse.setText(measurementItem.getNameHorse());
            if(measurementItem.getSelected())
                holder.imageSelected.setImageResource(R.drawable.ic_sel_item);
            else
                holder.imageSelected.setImageResource(R.drawable.ic_unsel_item);

            return convertView;
        }
        catch (Exception e) {
            Log.e(TAG, e.getMessage());
            return null;
        }
	}
}
